package group32.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.event.dom.client.KeyPressHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.FormPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

public class EditSecurity extends Composite{
	int userid=-1;
	private EditProfileServiceAsync pService = GWT
	.create(EditProfileService.class);
	
	/**
	 * The message displayed to the user when the server cannot be reached or
	 * returns an error.
	 */
	private static final String SERVER_ERROR = "An error occurred while "
			+ "attempting to contact the server. Please check your network "
			+ "connection and try again.";
	
	  private VerticalPanel mainPanel = new VerticalPanel();
	  FormPanel update = new FormPanel();
	  private VerticalPanel contentPanel = new VerticalPanel();
	  private VerticalPanel pwdPanel = new VerticalPanel();
	  private VerticalPanel chkPwdPanel = new VerticalPanel();
	  private VerticalPanel msgPanel = new VerticalPanel();
	  private VerticalPanel oldPPanel = new VerticalPanel();
	  private HorizontalPanel addPanel = new HorizontalPanel();
	  private ListBox userType = new ListBox();
	  final Label notifyLabel = new Label();
	  final Label titleLabel = new Label();
	  final Label errorLabel = new Label();
		final Button sendButton = new Button("Update");
		@SuppressWarnings("deprecation")
		final Button resetButton = new Button("Cancel",UserProfile.listner);
		final TextBox nameField = new TextBox();
		final TextBox firstName = new TextBox();
		final TextBox lastName = new TextBox();
		final TextBox email = new TextBox();
		final PasswordTextBox oldpassword = new PasswordTextBox();
		final PasswordTextBox password = new PasswordTextBox();
		final PasswordTextBox chkPassword = new PasswordTextBox();

		Hyperlink link1 = new Hyperlink("User Profile Page","userprofile");
		Hyperlink link2 = new Hyperlink("Edit Profile Details", "edituser");
		Hyperlink link3 = new Hyperlink("User Registration", "reguser");
	  
	/**
	 * This is the entry point method.
	 */
	public EditSecurity(final int uid,final String username) {
		
		//Window.setTitle("Update Security Credentials");	
		// We can add style names to widgets
		sendButton.addStyleName("sendButton");

		// Add the nameField and sendButton to the RootPanel
	
		addPanel.add(sendButton);
		addPanel.add(resetButton);
	
		mainPanel.add(new Label(
		        "Please enter your username:"));
		mainPanel.add(nameField);
		mainPanel.add(new Label("Enter your old password:"));
		mainPanel.add(oldpassword);
		mainPanel.add(new Label("Enter your new password:"));
		mainPanel.add(password);
		mainPanel.add(new Label("Re-enter your password:"));
		mainPanel.add(chkPassword);		
		mainPanel.addStyleName("paddedHorizontalbottom");
		update.add(mainPanel);
		contentPanel.add(update);
		contentPanel.add(new HTML("</div>"+
			        "<div align='center' colspan='2' valign='bottom'>"));
		contentPanel.add(addPanel);
		contentPanel.add(new HTML("</div>"+
		        "<div colspan='2' style='color:red;' id='errorLabelContainer'>"));
		contentPanel.add(msgPanel);
		contentPanel.add(new HTML("</div>"+
		        "<div colspan='2' style='color:green;' id='notificationContainer'>"));
		contentPanel.add(notifyLabel);
		// Focus the cursor on the name field when the app loads
		nameField.setFocus(true);
		nameField.selectAll();
		
		initWidget(contentPanel);

	

		// Add a handler to add a user
		sendButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
			updateSecurity(uid,nameField.getText(), oldpassword.getText(),password.getText());
			}

		
		});

		sendButton.addKeyPressHandler(new KeyPressHandler() {
			
			@Override
			public void onKeyPress(KeyPressEvent event) {
				 if (event.getCharCode() == KeyCodes.KEY_ENTER) {
					updateSecurity(uid,nameField.getText(), oldpassword.getText(),password.getText());
			        }
				
			}
		});
	

	}
	
	
	public String greetServer(String name) throws IllegalArgumentException {
		// TODO Auto-generated method stub
		return null;
	}
	  /**
	   * Add user to the db. Executed when the user clicks the register button or
	   * presses enter.
	   */
	  public boolean updateSecurity(final int uid,final String username,String password,String newPass)
			  throws IllegalArgumentException {

		  final boolean success=false;
		  
		  notifyLabel.setText("Sending Data....");
		  //msgPanel.add(notifyLabel);
		 // RootPanel.get("notificationContainer").add(notifyLabel);

	    // TODO Auto-generated method stub
        pService.updateUserSecurity(username,password,newPass, new AsyncCallback<Boolean>() {
                public void onSuccess(Boolean result) {
              	  //resetFields();
              	  //RootPanel.get("notificationContainer").clear();
              	  Window.alert("You have sucessfully changed your password");
              	  UserProfile.dialogBox.hide();
              	  //boolean success=result;
          		 // notifyLabel.setText("User has been successfully saved");
                }
                
                public void onFailure(Throwable caught) {
              	  //RootPanel.get("notificationContainer").clear();
          		  errorLabel.setText("There was an error saving to database");
          		  msgPanel.add(errorLabel);
          		  RootPanel.get("notificationContainer").add(errorLabel);
                }
               
                
        });
        return success;
	  }
	  
	
}
